package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.SysConfig;
import com.itheima.ydd.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/sys_config")
@CrossOrigin
@Slf4j
public class SysConfigController {
    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 后端系统设置明细
     * @return
     */
    @GetMapping("/all")
    public R getAll() {

        //设置list
        List<Map<String, Object>> list = sysConfigService.getList();

        //设置configRules
        Map<String, Object> configRules = new HashMap<>();
        LambdaQueryWrapper<SysConfig> validatorWrapper = new LambdaQueryWrapper<>();
        validatorWrapper.select(SysConfig::getValidator);
        List<SysConfig> configsValidator = sysConfigService.list(validatorWrapper);

        StringBuilder configRuleStr = new StringBuilder("");
        for (SysConfig sysConfig : configsValidator) {
            String validator = sysConfig.getValidator();
            if (StringUtils.isNotEmpty(validator)) {
                configRuleStr.append(configRuleStr.length() > 0 ? "," + validator : validator);
            }
        }
        configRules.put("site_name", configRuleStr.toString());

        //设置configForm
        Map<String, Object> configForm = new HashMap<>();
        List<SysConfig> configList = sysConfigService.list();
        for (SysConfig sysConfig : configList) {
            configForm.put(sysConfig.getAttrKey(), sysConfig.getAttrValue());
        }

        //设置alldata
        for (SysConfig sysConfig : configList) {
            if (sysConfig.getId() == 125) {
                List<Map<String, Object>> sysConfig125 = sysConfigService.get125ConfigValue();
                sysConfig.setConfigValue(sysConfig125);
            }
        }

        //封装成map
        Map<String, Object> data = new HashMap<>();
        R r =new R();
        r.putData("list", list);
        r.putData("allData", configList);
        r.putData("configForm", configForm);
        r.putData("configRules", configRules);
        r.setErrno(0);
        r.setErrmsg("");
        return r;
    }


    /**
     * 修改数据
     *
     * @param configList
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody List<SysConfig> configList) {

        sysConfigService.updateBatchById(configList);

        return ResultData.ok("","");

    }


    /**
     * 新增数据(前端貌似没用到)
     *
     * @param sysConfig
     * @return
     */
    @PostMapping("/add")
    public ResultData save(@RequestBody SysConfig sysConfig) {

        sysConfigService.save(sysConfig);
        return ResultData.ok("","");

    }



}
